<!doctype html public "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
<html>
<head>
<title>
JSJaC Overview
</title>
<link rel ="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script>
function asd() {
	
		parent.document.title="JSJaCHttpBindingConnection.js Overview";
	
}
</script>
</head>
<body bgcolor="white" onload="asd();">

<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> 	<font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top">
<em>
<b>JSJaC</b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<center>
	
	   <h2>JSJaCHttpBindingConnection.js</h2>
	
</center>

	


<h4>Summary</h4>
<p>
	
		All stuff related to HTTP Binding
 <BR/><BR/><B>Version: </B>$Revision: 460 $<BR/><BR/><B>Author:</B> Stefan Strigler steve@zeank.in-berlin.de
 <BR/>
	
</p>

<hr>


    <table border="1" cellpadding="3" cellspacing="0" width="100%">
    <tr bgcolor="#CCCCFF" class="TableHeadingColor">
    <td colspan=2><font size="+2">
    
        <b>Class Summary</b>
    
    </font></td>
    </tr>
    
    <tr bgcolor="white" class="TableRowColor">
    <td width="15%"><b><a href="JSJaCHttpBindingConnection.html">JSJaCHttpBindingConnection</a></b></td>
    <td>Implementation of <a href='http://www.xmpp.org/extensions/xep-0206.html'>XMPP Over BOSH</a>
 formerly known as HTTP Binding.</td>
    </tr>
    
    </table>
    <hr/> 


<!-- ========== METHOD SUMMARY =========== -->

<!-- ========== END METHOD SUMMARY =========== -->


        <pre class="sourceview"><span class="comment">/**
 * <span class="attrib">@fileoverview</span> All stuff related to HTTP Binding
 * <span class="attrib">@author</span> Stefan Strigler steve<span class="attrib">@zeank</span>.in-berlin.de
 * <span class="attrib">@version</span> $Revision: 460 $
 */</span>

<span class="comment">/**
 * Instantiates an HTTP Binding session
 * <span class="attrib">@class</span> Implementation of {<span class="attrib">@link</span>
 * http://www.xmpp.org/extensions/xep-0206.html XMPP Over BOSH}
 * formerly known as HTTP Binding.
 * <span class="attrib">@extends</span> JSJaCConnection
 * <span class="attrib">@constructor</span>
 */</span>
<span class="reserved">function</span> JSJaCHttpBindingConnection(oArg) {
  <span class="comment">/**
   * <span class="attrib">@ignore</span>
   */</span>
  <span class="reserved">this</span>.base = JSJaCConnection;
  <span class="reserved">this</span>.base(oArg);

  <span class="comment">// member vars</span>
  <span class="comment">/**
   * <span class="attrib">@private</span>
   */</span>
  <span class="reserved">this</span>._hold = JSJACHBC_MAX_HOLD;
  <span class="comment">/**
   * <span class="attrib">@private</span>
   */</span>
  <span class="reserved">this</span>._inactivity = 0;
  <span class="comment">/**
   * <span class="attrib">@private</span>
   */</span>
  <span class="reserved">this</span>._last_requests = new Object(); <span class="comment">// 'hash' storing hold+1 last requests</span>
  <span class="comment">/**
   * <span class="attrib">@private</span>
   */</span>
  <span class="reserved">this</span>._last_rid = 0;                 <span class="comment">// I know what you did last summer</span>
  <span class="comment">/**
   * <span class="attrib">@private</span>
   */</span>
  <span class="reserved">this</span>._min_polling = 0;

  <span class="comment">/**
   * <span class="attrib">@private</span>
   */</span>
  <span class="reserved">this</span>._pause = 0;
  <span class="comment">/**
   * <span class="attrib">@private</span>
   */</span>
  <span class="reserved">this</span>._wait = JSJACHBC_MAX_WAIT;
}
JSJaCHttpBindingConnection.<span class="reserved">prototype</span> = new JSJaCConnection();

<span class="comment">/**
 * Inherit an instantiated HTTP Binding session
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>.inherit = <span class="reserved">function</span>(oArg) {
  <span class="reserved">this</span>.domain = oArg.domain || <span class="literal">'localhost'</span>;
  <span class="reserved">this</span>.username = oArg.username;
  <span class="reserved">this</span>.resource = oArg.resource;
  <span class="reserved">this</span>._sid = oArg.sid;
  <span class="reserved">this</span>._rid = oArg.rid;
  <span class="reserved">this</span>._min_polling = oArg.polling;
  <span class="reserved">this</span>._inactivity = oArg.inactivity;
  <span class="reserved">this</span>._setHold(oArg.requests-1);
  <span class="reserved">this</span>.setPollInterval(<span class="reserved">this</span>._timerval);
  <span class="reserved">if</span> (oArg.wait)
    <span class="reserved">this</span>._wait = oArg.wait; <span class="comment">// for whatever reason</span>

  <span class="reserved">this</span>._connected = true;

  <span class="reserved">this</span>._handleEvent(<span class="literal">'onconnect'</span>);

  <span class="reserved">this</span>._interval= setInterval(JSJaC.bind(<span class="reserved">this</span>._checkQueue, <span class="reserved">this</span>),
                              JSJAC_CHECKQUEUEINTERVAL);
  <span class="reserved">this</span>._inQto = setInterval(JSJaC.bind(<span class="reserved">this</span>._checkInQ, <span class="reserved">this</span>),
                            JSJAC_CHECKINQUEUEINTERVAL);
  <span class="reserved">this</span>._timeout = setTimeout(JSJaC.bind(<span class="reserved">this</span>._process, <span class="reserved">this</span>),
                             <span class="reserved">this</span>.getPollInterval());
};

<span class="comment">/**
 * Sets poll interval
 * <span class="attrib">@param</span> {int} timerval the interval in seconds
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>.setPollInterval = <span class="reserved">function</span>(timerval) {
  <span class="reserved">if</span> (timerval &amp;&amp; !isNaN(timerval)) {
    <span class="reserved">if</span> (!<span class="reserved">this</span>.isPolling())
      <span class="reserved">this</span>._timerval = 100;
    <span class="reserved">else</span> <span class="reserved">if</span> (<span class="reserved">this</span>._min_polling &amp;&amp; timerval &lt; <span class="reserved">this</span>._min_polling*1000)
      <span class="reserved">this</span>._timerval = <span class="reserved">this</span>._min_polling*1000;
    <span class="reserved">else</span> <span class="reserved">if</span> (<span class="reserved">this</span>._inactivity &amp;&amp; timerval &gt; <span class="reserved">this</span>._inactivity*1000)
      <span class="reserved">this</span>._timerval = <span class="reserved">this</span>._inactivity*1000;
    <span class="reserved">else</span>
      <span class="reserved">this</span>._timerval = timerval;
  }
  <span class="reserved">return</span> <span class="reserved">this</span>._timerval;
};

<span class="comment">/**
 * whether this session is in polling mode
 * <span class="attrib">@type</span> boolean
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>.isPolling = <span class="reserved">function</span>() { <span class="reserved">return</span> (<span class="reserved">this</span>._hold == 0) };

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._getFreeSlot = <span class="reserved">function</span>() {
  <span class="reserved">for</span> (var i=0; i&lt;<span class="reserved">this</span>._hold+1; i++)
    <span class="reserved">if</span> (typeof(<span class="reserved">this</span>._req[i]) == <span class="literal">'undefined'</span> || typeof(<span class="reserved">this</span>._req[i].r) == <span class="literal">'undefined'</span> || <span class="reserved">this</span>._req[i].r.readyState == 4)
      <span class="reserved">return</span> i;
  <span class="reserved">return</span> -1; <span class="comment">// nothing found</span>
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._getHold = <span class="reserved">function</span>() { <span class="reserved">return</span> <span class="reserved">this</span>._hold; };

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._getRequestString = <span class="reserved">function</span>(raw, last) {
  raw = raw || <span class="literal">''</span>;
  var reqstr = <span class="literal">''</span>;

  <span class="comment">// check if we're repeating a request</span>

  <span class="reserved">if</span> (<span class="reserved">this</span>._rid &lt;= <span class="reserved">this</span>._last_rid &amp;&amp; typeof(<span class="reserved">this</span>._last_requests[<span class="reserved">this</span>._rid]) != <span class="literal">'undefined'</span>) <span class="comment">// repeat!</span>
    reqstr = <span class="reserved">this</span>._last_requests[<span class="reserved">this</span>._rid].xml;
  <span class="reserved">else</span> { <span class="comment">// grab from queue</span>
    var xml = <span class="literal">''</span>;
    <span class="reserved">while</span> (<span class="reserved">this</span>._pQueue.length) {
      var curNode = <span class="reserved">this</span>._pQueue[0];
      xml += curNode;
      <span class="reserved">this</span>._pQueue = <span class="reserved">this</span>._pQueue.slice(1,<span class="reserved">this</span>._pQueue.length);
    }

    reqstr = <span class="literal">"&lt;body rid='"</span>+<span class="reserved">this</span>._rid+<span class="literal">"' sid='"</span>+<span class="reserved">this</span>._sid+<span class="literal">"' xmlns='http://jabber.org/protocol/httpbind' "</span>;
    <span class="reserved">if</span> (JSJAC_HAVEKEYS) {
      reqstr += <span class="literal">"key='"</span>+<span class="reserved">this</span>._keys.getKey()+<span class="literal">"' "</span>;
      <span class="reserved">if</span> (<span class="reserved">this</span>._keys.lastKey()) {
        <span class="reserved">this</span>._keys = new JSJaCKeys(hex_sha1,<span class="reserved">this</span>.oDbg);
        reqstr += <span class="literal">"newkey='"</span>+<span class="reserved">this</span>._keys.getKey()+<span class="literal">"' "</span>;
      }
    }
    <span class="reserved">if</span> (last)
      reqstr += <span class="literal">"type='terminate' "</span>;
    <span class="reserved">else</span> <span class="reserved">if</span> (<span class="reserved">this</span>._reinit) {
      <span class="reserved">if</span> (JSJACHBC_USE_BOSH_VER) 
        reqstr += <span class="literal">"xmpp:restart='true' "</span>;
      <span class="reserved">this</span>._reinit = false;
    }

    <span class="reserved">if</span> (xml != <span class="literal">''</span> || raw != <span class="literal">''</span>) {
      reqstr += <span class="literal">"&gt;"</span> + raw + xml + <span class="literal">"&lt;/body&gt;"</span>;
    } <span class="reserved">else</span> {
      reqstr += <span class="literal">"/&gt;"</span>;
    }

    <span class="reserved">this</span>._last_requests[<span class="reserved">this</span>._rid] = new Object();
    <span class="reserved">this</span>._last_requests[<span class="reserved">this</span>._rid].xml = reqstr;
    <span class="reserved">this</span>._last_rid = <span class="reserved">this</span>._rid;

    <span class="reserved">for</span> (var i in <span class="reserved">this</span>._last_requests)
      <span class="reserved">if</span> (<span class="reserved">this</span>._last_requests.hasOwnProperty(i) &amp;&amp;
          i &lt; <span class="reserved">this</span>._rid-<span class="reserved">this</span>._hold)
        delete(<span class="reserved">this</span>._last_requests[i]); <span class="comment">// truncate</span>
  }
	
  <span class="reserved">return</span> reqstr;
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._getInitialRequestString = <span class="reserved">function</span>() {
  var reqstr = <span class="literal">"&lt;body hold='"</span>+<span class="reserved">this</span>._hold+<span class="literal">"' xmlns='http://jabber.org/protocol/httpbind' to='"</span>+<span class="reserved">this</span>.authhost+<span class="literal">"' wait='"</span>+<span class="reserved">this</span>._wait+<span class="literal">"' rid='"</span>+<span class="reserved">this</span>._rid+<span class="literal">"'"</span>;
  <span class="reserved">if</span> (<span class="reserved">this</span>.host || <span class="reserved">this</span>.port)
    reqstr += <span class="literal">" route='xmpp:"</span>+<span class="reserved">this</span>.host+<span class="literal">":"</span>+<span class="reserved">this</span>.port+<span class="literal">"'"</span>;
  <span class="reserved">if</span> (<span class="reserved">this</span>.secure)
    reqstr += <span class="literal">" secure='"</span>+<span class="reserved">this</span>.secure+<span class="literal">"'"</span>;
  <span class="reserved">if</span> (JSJAC_HAVEKEYS) {
    <span class="reserved">this</span>._keys = new JSJaCKeys(hex_sha1,<span class="reserved">this</span>.oDbg); <span class="comment">// generate first set of keys</span>
    key = <span class="reserved">this</span>._keys.getKey();
    reqstr += <span class="literal">" newkey='"</span>+key+<span class="literal">"'"</span>;
  }
  <span class="reserved">if</span> (<span class="reserved">this</span>._xmllang)
    reqstr += <span class="literal">" xml:lang='"</span>+<span class="reserved">this</span>._xmllang + <span class="literal">"'"</span>;

  <span class="reserved">if</span> (JSJACHBC_USE_BOSH_VER) {
    reqstr += <span class="literal">" ver='"</span> + JSJACHBC_BOSH_VERSION + <span class="literal">"'"</span>;
    reqstr += <span class="literal">" xmpp:xmlns='urn:xmpp:xbosh'"</span>;
    <span class="reserved">if</span> (<span class="reserved">this</span>.authtype == <span class="literal">'sasl'</span> || <span class="reserved">this</span>.authtype == <span class="literal">'saslanon'</span>)
      reqstr += <span class="literal">" xmpp:version='1.0'"</span>;
  }
  reqstr += <span class="literal">"/&gt;"</span>;
  <span class="reserved">return</span> reqstr;
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._getStreamID = <span class="reserved">function</span>(slot) {

  <span class="reserved">this</span>.oDbg.log(<span class="reserved">this</span>._req[slot].r.responseText,4);

  <span class="reserved">if</span> (!<span class="reserved">this</span>._req[slot].r.responseXML || !<span class="reserved">this</span>._req[slot].r.responseXML.documentElement) {
    <span class="reserved">this</span>._handleEvent(<span class="literal">'onerror'</span>,JSJaCError(<span class="literal">'503'</span>,<span class="literal">'cancel'</span>,<span class="literal">'service-unavailable'</span>));
    <span class="reserved">return</span>;
  }
  var body = <span class="reserved">this</span>._req[slot].r.responseXML.documentElement;

  <span class="comment">// extract stream id used for non-SASL authentication</span>
  <span class="reserved">if</span> (body.getAttribute(<span class="literal">'authid'</span>)) {
    <span class="reserved">this</span>.streamid = body.getAttribute(<span class="literal">'authid'</span>);
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"got streamid: "</span>+<span class="reserved">this</span>.streamid,2);
  } <span class="reserved">else</span> {
    <span class="reserved">this</span>._timeout = setTimeout(JSJaC.bind(<span class="reserved">this</span>._sendEmpty, <span class="reserved">this</span>),
                               <span class="reserved">this</span>.getPollInterval());
    <span class="reserved">return</span>;
  }

  <span class="reserved">this</span>._timeout = setTimeout(JSJaC.bind(<span class="reserved">this</span>._process, <span class="reserved">this</span>),
                             <span class="reserved">this</span>.getPollInterval());

  <span class="reserved">if</span> (!<span class="reserved">this</span>._parseStreamFeatures(body))
    <span class="reserved">return</span>;

  <span class="reserved">if</span> (<span class="reserved">this</span>.register)
    <span class="reserved">this</span>._doInBandReg();
  <span class="reserved">else</span>
    <span class="reserved">this</span>._doAuth();
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._getSuspendVars = <span class="reserved">function</span>() {
  <span class="reserved">return</span> (<span class="literal">'host,port,secure,_rid,_last_rid,_wait,_min_polling,_inactivity,_hold,_last_requests,_pause'</span>).split(<span class="literal">','</span>);
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._handleInitialResponse = <span class="reserved">function</span>(slot) {
  try {
    <span class="comment">// This will throw an error on Mozilla when the connection was refused</span>
    <span class="reserved">this</span>.oDbg.log(<span class="reserved">this</span>._req[slot].r.getAllResponseHeaders(),4);
    <span class="reserved">this</span>.oDbg.log(<span class="reserved">this</span>._req[slot].r.responseText,4);
  } catch(ex) {
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"No response"</span>,4);
  }

  <span class="reserved">if</span> (<span class="reserved">this</span>._req[slot].r.status != 200 || !<span class="reserved">this</span>._req[slot].r.responseXML) {
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"initial response broken (status: "</span>+<span class="reserved">this</span>._req[slot].r.status+<span class="literal">")"</span>,1);
    <span class="reserved">this</span>._handleEvent(<span class="literal">'onerror'</span>,JSJaCError(<span class="literal">'503'</span>,<span class="literal">'cancel'</span>,<span class="literal">'service-unavailable'</span>));
    <span class="reserved">return</span>;
  }
  var body = <span class="reserved">this</span>._req[slot].r.responseXML.documentElement;

  <span class="reserved">if</span> (!body || body.tagName != <span class="literal">'body'</span> || body.namespaceURI != <span class="literal">'http://jabber.org/protocol/httpbind'</span>) {
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"no body element or incorrect body in initial response"</span>,1);
    <span class="reserved">this</span>._handleEvent(<span class="literal">"onerror"</span>,JSJaCError(<span class="literal">"500"</span>,<span class="literal">"wait"</span>,<span class="literal">"internal-service-error"</span>));
    <span class="reserved">return</span>;
  }

  <span class="comment">// Check for errors from the server</span>
  <span class="reserved">if</span> (body.getAttribute(<span class="literal">"type"</span>) == <span class="literal">"terminate"</span>) {
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"invalid response:\n"</span> + <span class="reserved">this</span>._req[slot].r.responseText,1);
    clearTimeout(<span class="reserved">this</span>._timeout); <span class="comment">// remove timer</span>
    <span class="reserved">this</span>._connected = false;
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"Disconnected."</span>,1);
    <span class="reserved">this</span>._handleEvent(<span class="literal">'ondisconnect'</span>);
    <span class="reserved">this</span>._handleEvent(<span class="literal">'onerror'</span>,JSJaCError(<span class="literal">'503'</span>,<span class="literal">'cancel'</span>,<span class="literal">'service-unavailable'</span>));
    <span class="reserved">return</span>;
  }

  <span class="comment">// get session ID</span>
  <span class="reserved">this</span>._sid = body.getAttribute(<span class="literal">'sid'</span>);
  <span class="reserved">this</span>.oDbg.log(<span class="literal">"got sid: "</span>+<span class="reserved">this</span>._sid,2);

  <span class="comment">// get attributes from response body</span>
  <span class="reserved">if</span> (body.getAttribute(<span class="literal">'polling'</span>))
    <span class="reserved">this</span>._min_polling = body.getAttribute(<span class="literal">'polling'</span>);

  <span class="reserved">if</span> (body.getAttribute(<span class="literal">'inactivity'</span>))
    <span class="reserved">this</span>._inactivity = body.getAttribute(<span class="literal">'inactivity'</span>);

  <span class="reserved">if</span> (body.getAttribute(<span class="literal">'requests'</span>))
    <span class="reserved">this</span>._setHold(body.getAttribute(<span class="literal">'requests'</span>)-1);
  <span class="reserved">this</span>.oDbg.log(<span class="literal">"set hold to "</span> + <span class="reserved">this</span>._getHold(),2);

  <span class="reserved">if</span> (body.getAttribute(<span class="literal">'ver'</span>))
    <span class="reserved">this</span>._bosh_version = body.getAttribute(<span class="literal">'ver'</span>);

  <span class="reserved">if</span> (body.getAttribute(<span class="literal">'maxpause'</span>))
    <span class="reserved">this</span>._pause = Number.max(body.getAttribute(<span class="literal">'maxpause'</span>), JSJACHBC_MAXPAUSE);

  <span class="comment">// must be done after response attributes have been collected</span>
  <span class="reserved">this</span>.setPollInterval(<span class="reserved">this</span>._timerval);

  <span class="comment">/* start sending from queue for not polling connections */</span>
  <span class="reserved">this</span>._connected = true;

  <span class="reserved">this</span>._inQto = setInterval(JSJaC.bind(<span class="reserved">this</span>._checkInQ, <span class="reserved">this</span>),
                            JSJAC_CHECKINQUEUEINTERVAL);
  <span class="reserved">this</span>._interval= setInterval(JSJaC.bind(<span class="reserved">this</span>._checkQueue, <span class="reserved">this</span>),
                              JSJAC_CHECKQUEUEINTERVAL);

  <span class="comment">/* wait for initial stream response to extract streamid needed
   * for digest auth
   */</span>
  <span class="reserved">this</span>._getStreamID(slot);
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._parseResponse = <span class="reserved">function</span>(req) {
  <span class="reserved">if</span> (!<span class="reserved">this</span>.connected() || !req)
    <span class="reserved">return</span> null;

  var r = req.r; <span class="comment">// the XmlHttpRequest</span>

  try {
    <span class="reserved">if</span> (r.status == 404 || r.status == 403) {
      <span class="comment">// connection manager killed session</span>
      <span class="reserved">this</span>._abort();
      <span class="reserved">return</span> null;
    }

    <span class="reserved">if</span> (r.status != 200 || !r.responseXML) {
      <span class="reserved">this</span>._errcnt++;
      var errmsg = <span class="literal">"invalid response ("</span>+r.status+<span class="literal">"):\n"</span> + r.getAllResponseHeaders()+<span class="literal">"\n"</span>+r.responseText;
      <span class="reserved">if</span> (!r.responseXML)
        errmsg += <span class="literal">"\nResponse failed to parse!"</span>;
      <span class="reserved">this</span>.oDbg.log(errmsg,1);
      <span class="reserved">if</span> (<span class="reserved">this</span>._errcnt &gt; JSJAC_ERR_COUNT) {
        <span class="comment">// abort</span>
        <span class="reserved">this</span>._abort();
        <span class="reserved">return</span> null;
      }
      <span class="reserved">this</span>.oDbg.log(<span class="literal">"repeating ("</span>+<span class="reserved">this</span>._errcnt+<span class="literal">")"</span>,1);
     
      <span class="reserved">this</span>._setStatus(<span class="literal">'proto_error_fallback'</span>);
     
      <span class="comment">// schedule next tick</span>
      setTimeout(JSJaC.bind(<span class="reserved">this</span>._resume, <span class="reserved">this</span>),
                 <span class="reserved">this</span>.getPollInterval());
     
      <span class="reserved">return</span> null;
    }
  } catch (e) {
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"XMLHttpRequest error: status not available"</span>, 1);
	<span class="reserved">this</span>._errcnt++;
	<span class="reserved">if</span> (<span class="reserved">this</span>._errcnt &gt; JSJAC_ERR_COUNT) {
	  <span class="comment">// abort</span>
	  <span class="reserved">this</span>._abort();
	} <span class="reserved">else</span> {
	  <span class="reserved">this</span>.oDbg.log(<span class="literal">"repeating ("</span>+<span class="reserved">this</span>._errcnt+<span class="literal">")"</span>,1);
     
	  <span class="reserved">this</span>._setStatus(<span class="literal">'proto_error_fallback'</span>);
     
	  <span class="comment">// schedule next tick</span>
	  setTimeout(JSJaC.bind(<span class="reserved">this</span>._resume, <span class="reserved">this</span>),
                     <span class="reserved">this</span>.getPollInterval()); 
    }
    <span class="reserved">return</span> null;
  }

  var body = r.responseXML.documentElement;
  <span class="reserved">if</span> (!body || body.tagName != <span class="literal">'body'</span> ||
	  body.namespaceURI != <span class="literal">'http://jabber.org/protocol/httpbind'</span>) {
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"invalid response:\n"</span> + r.responseText,1);

    clearTimeout(<span class="reserved">this</span>._timeout); <span class="comment">// remove timer</span>
    clearInterval(<span class="reserved">this</span>._interval);
    clearInterval(<span class="reserved">this</span>._inQto);

    <span class="reserved">this</span>._connected = false;
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"Disconnected."</span>,1);
    <span class="reserved">this</span>._handleEvent(<span class="literal">'ondisconnect'</span>);

    <span class="reserved">this</span>._setStatus(<span class="literal">'internal_server_error'</span>);
    <span class="reserved">this</span>._handleEvent(<span class="literal">'onerror'</span>,
					  JSJaCError(<span class="literal">'500'</span>,<span class="literal">'wait'</span>,<span class="literal">'internal-server-error'</span>));

    <span class="reserved">return</span> null;
  }

  <span class="reserved">if</span> (typeof(req.rid) != <span class="literal">'undefined'</span> &amp;&amp; <span class="reserved">this</span>._last_requests[req.rid]) {
    <span class="reserved">if</span> (<span class="reserved">this</span>._last_requests[req.rid].handled) {
      <span class="reserved">this</span>.oDbg.log(<span class="literal">"already handled "</span>+req.rid,2);
      <span class="reserved">return</span> null;
    } <span class="reserved">else</span>
      <span class="reserved">this</span>._last_requests[req.rid].handled = true;
  }


  <span class="comment">// Check for errors from the server</span>
  <span class="reserved">if</span> (body.getAttribute(<span class="literal">"type"</span>) == <span class="literal">"terminate"</span>) {
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"session terminated:\n"</span> + r.responseText,1);

    clearTimeout(<span class="reserved">this</span>._timeout); <span class="comment">// remove timer</span>
    clearInterval(<span class="reserved">this</span>._interval);
    clearInterval(<span class="reserved">this</span>._inQto);

    <span class="reserved">if</span> (body.getAttribute(<span class="literal">"condition"</span>) == <span class="literal">"remote-stream-error"</span>)
      <span class="reserved">if</span> (body.getElementsByTagName(<span class="literal">"conflict"</span>).length &gt; 0)
        <span class="reserved">this</span>._setStatus(<span class="literal">"session-terminate-conflict"</span>);
    <span class="reserved">this</span>._handleEvent(<span class="literal">'onerror'</span>,JSJaCError(<span class="literal">'503'</span>,<span class="literal">'cancel'</span>,body.getAttribute(<span class="literal">'condition'</span>)));
    <span class="reserved">this</span>._connected = false;
    <span class="reserved">this</span>.oDbg.log(<span class="literal">"Disconnected."</span>,1);
    <span class="reserved">this</span>._handleEvent(<span class="literal">'ondisconnect'</span>);
    <span class="reserved">return</span> null;
  }

  <span class="comment">// no error</span>
  <span class="reserved">this</span>._errcnt = 0;
  <span class="reserved">return</span> r.responseXML.documentElement;
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._reInitStream = <span class="reserved">function</span>(to,cb,arg) {
  <span class="comment">/* [TODO] we can't handle 'to' here as this is not (yet) supported
   * by the protocol
   */</span>

  <span class="comment">// tell http binding to reinit stream with/before next request</span>
  <span class="reserved">this</span>._reinit = true;
  cb.call(<span class="reserved">this</span>,arg); <span class="comment">// proceed with next callback</span>

  <span class="comment">/* [TODO] make sure that we're checking for new stream features when
   * 'cb' finishes
   */</span>
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._resume = <span class="reserved">function</span>() {
  <span class="comment">/* make sure to repeat last request as we can be sure that
   * it had failed (only if we're not using the 'pause' attribute
   */</span>
  <span class="reserved">if</span> (<span class="reserved">this</span>._pause == 0 &amp;&amp; <span class="reserved">this</span>._rid &gt;= <span class="reserved">this</span>._last_rid)
    <span class="reserved">this</span>._rid = <span class="reserved">this</span>._last_rid-1;

  <span class="reserved">this</span>._process();
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._setHold = <span class="reserved">function</span>(hold)  {
  <span class="reserved">if</span> (!hold || isNaN(hold) || hold &lt; 0)
    hold = 0;
  <span class="reserved">else</span> <span class="reserved">if</span> (hold &gt; JSJACHBC_MAX_HOLD)
    hold = JSJACHBC_MAX_HOLD;
  <span class="reserved">this</span>._hold = hold;
  <span class="reserved">return</span> <span class="reserved">this</span>._hold;
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._setupRequest = <span class="reserved">function</span>(async) {
  var req = new Object();
  var r = XmlHttp.create();
  try {
    r.open(<span class="literal">"POST"</span>,<span class="reserved">this</span>._httpbase,async);
    r.setRequestHeader(<span class="literal">'Content-Type'</span>,<span class="literal">'text/xml; charset=utf-8'</span>);
  } catch(e) { <span class="reserved">this</span>.oDbg.log(e,1); }
  req.r = r;
  <span class="reserved">this</span>._rid++;
  req.rid = <span class="reserved">this</span>._rid;
  <span class="reserved">return</span> req;
};

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
JSJaCHttpBindingConnection.<span class="reserved">prototype</span>._suspend = <span class="reserved">function</span>() {
  <span class="reserved">if</span> (<span class="reserved">this</span>._pause == 0)
    <span class="reserved">return</span>; <span class="comment">// got nothing to do</span>

  var slot = <span class="reserved">this</span>._getFreeSlot();
  <span class="comment">// Intentionally synchronous</span>
  <span class="reserved">this</span>._req[slot] = <span class="reserved">this</span>._setupRequest(false);

  var reqstr = <span class="literal">"&lt;body pause='"</span>+<span class="reserved">this</span>._pause+<span class="literal">"' xmlns='http://jabber.org/protocol/httpbind' sid='"</span>+<span class="reserved">this</span>._sid+<span class="literal">"' rid='"</span>+<span class="reserved">this</span>._rid+<span class="literal">"'"</span>;
  <span class="reserved">if</span> (JSJAC_HAVEKEYS) {
    reqstr += <span class="literal">" key='"</span>+<span class="reserved">this</span>._keys.getKey()+<span class="literal">"'"</span>;
    <span class="reserved">if</span> (<span class="reserved">this</span>._keys.lastKey()) {
      <span class="reserved">this</span>._keys = new JSJaCKeys(hex_sha1,<span class="reserved">this</span>.oDbg);
      reqstr += <span class="literal">" newkey='"</span>+<span class="reserved">this</span>._keys.getKey()+<span class="literal">"'"</span>;
    }

  }
  reqstr += <span class="literal">"&gt;"</span>;

  <span class="reserved">while</span> (<span class="reserved">this</span>._pQueue.length) {
    var curNode = <span class="reserved">this</span>._pQueue[0];
    reqstr += curNode;
    <span class="reserved">this</span>._pQueue = <span class="reserved">this</span>._pQueue.slice(1,<span class="reserved">this</span>._pQueue.length);
  }

  <span class="comment">//reqstr += "&lt;presence type='unavailable' xmlns='jabber:client'/&gt;";</span>
  reqstr += <span class="literal">"&lt;/body&gt;"</span>;

  <span class="reserved">this</span>.oDbg.log(<span class="literal">"Disconnecting: "</span> + reqstr,4);
  <span class="reserved">this</span>._req[slot].r.send(reqstr);
};
</pre>
	<hr>



<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> <font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top"><em>
<b>JSJaC</b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<font size="-1">

</font>
<div class="jsdoc_ctime">Documentation generated by <a href="http://jsdoc.sourceforge.net/" target="_parent">JSDoc</a> on Tue Feb 12 18:46:55 2008</div>
</body>
</html>
